package com.dlyk.web;

import com.dlyk.model.TUser;
import com.dlyk.query.UserQuery;
import com.dlyk.result.R;
import com.dlyk.service.UserService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ajie
 * @date: 2024/7/9 20:57:11
 * @version: 1.0
 */
@RestController
public class UserController{

    @Autowired
    private UserService userService;
    
    /**
     * @description: 获取登陆人消息 
     * @params: [authentication]       
     * @return: 
     * @author: ajie
     * @date: 2024/7/9 21:12
     */
    @GetMapping("api/login/info")
    public R loginInfo(Authentication authentication){
        TUser tUser = (TUser) authentication.getPrincipal();
        return R.OK(tUser);
    }
    
    /**
     * @description: 免登录 
     * @params: []       
     * @return: 
     * @author: ajie
     * @date: 2024/7/12 16:44
     */
    @GetMapping("api/login/free")
    public R freeLogin(){
        return R.OK();
    }

    /**
     * @description: 用户列表分页查询
     * @params: [current]
     * @return:
     * @author: ajie
     * @date: 2024/7/13 23:14
     */
    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("api/users")
    public R userPage(@RequestParam(value = "current",required = false) Integer current){
        if (current == null){
            current = 1;
        }
        PageInfo<TUser> pageInfo = userService.getUserByPage(current);
        return R.OK(pageInfo);
    }

    /**
     * @description: 查询用户详情 
     * @params: [id]       
     * @return: 
     * @author: ajie
     * @date: 2024/7/14 18:02
     */
    @PreAuthorize("hasAuthority('user:view')")
    @GetMapping("api/user/{id}")
    public R userDetail(@PathVariable("id") Integer id){
        TUser tUser = userService.getUserById(id);
        return R.OK(tUser);
    }

    /**
     * @description: 新增用户
     * @params: [userQuery]       
     * @return:
     * @author: ajie
     * @date: 2024/7/15 21:25
     */
    @PreAuthorize("hasAuthority('user:add')")
    @PostMapping("api/user")
    public R addUser(UserQuery userQuery,@RequestHeader(value = "Authorization") String token){
        userQuery.setToken(token);
        int save = userService.saveUser(userQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 编辑用户
     * @params: [userQuery]
     * @return:
     * @author: ajie
     * @date: 2024/7/15 21:25
     */
    @PreAuthorize("hasAuthority('user:edit')")
    @PutMapping("api/user")
    public R editUser(UserQuery userQuery,@RequestHeader(value = "Authorization") String token){
        userQuery.setToken(token);
        int update = userService.updateUser(userQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 删除用户
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/7/17 18:14
     */
    @PreAuthorize("hasAuthority('user:delete')")
    @DeleteMapping("api/user/{id}")
    public R delUser(@PathVariable(value = "id") Integer id){
        int del = userService.delUserById(id);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 批量删除用户
     * @params: [ids]
     * @return:
     * @author: ajie
     * @date: 2024/7/18 18:39
     */
    @PreAuthorize("hasAuthority('user:delete')")
    @DeleteMapping("api/user")
    public R batchDelUser(@RequestParam(value = "ids") String ids){
        //ids = "1,3,5,7";
        List<String> idList = Arrays.asList(ids.split(","));
        int batchDel = userService.batchDelUserByIds(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }

    /**
     * @description: 查询所有负责人
     * @params: []
     * @return:
     * @author: ajie
     * @date: 2024/7/18 18:41
     */
    @GetMapping("api/owner")
    public R owner(){
        List<TUser> ownerList = userService.getOwnerList();
        return R.OK(ownerList);
    }


}
